package property.controller;

import com.alibaba.fastjson.JSONObject;
import property.pojo.Feedback;
import property.pojo.User;
import property.service.FeedbackService;
import property.service.impl.FeedbackServiceImpl;
import property.utils.servlet.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/Feedback/*"})
public class FeedbackServlet extends BaseServlet {
    private FeedbackService feedbackService = new FeedbackServiceImpl();

    public void addFeedback(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        String data = request.getParameter("data");
        JSONObject jb = JSONObject.parseObject(data);
        JSONObject jbReturn = new JSONObject();

        HttpSession session = request.getSession();
        short character = (short) session.getAttribute("loginType");
        if(character != 0){
            jbReturn.put("code",0);
            jbReturn.put("messages","权限不足！！");
            response.getWriter().write(jbReturn.toString());
            return;
        }


        Feedback feedback = JSONObject.toJavaObject(jb.getJSONObject("insert"),Feedback.class);
        feedback.setFbUserId(((User)session.getAttribute("currentUser")).getuId());
        int i = feedbackService.addFeedback(feedback);
        if(i > 0){
            jbReturn.put("code", 1);
            jbReturn.put("messages", "新增反馈成功");
        }else {
            jbReturn.put("code", 0);
            jbReturn.put("messages", "操作失败！");
        }
        response.getWriter().write(jbReturn.toString());
    }

//    public void deleteFeedback(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("application/json;charset=UTF-8");
//        String data = request.getParameter("data");
//        JSONObject jb = JSONObject.parseObject(data);
//        JSONObject jbReturn = new JSONObject();
//
//        HttpSession session = request.getSession();
//        short character = (short) session.getAttribute("loginType");
//        if(character == 1){
//            jbReturn.put("code",0);
//            jbReturn.put("messages","权限不足！！");
//            response.getWriter().write(jbReturn.toString());
//            return;
//        }
//
//        int i = feedbackService.deleteFeedback(jb.getInteger("fbId"));
//        if(i > 0){
//            jbReturn.put("code", 1);
//            jbReturn.put("messages", "删除反馈成功");
//        } else if (i < 0) {
//            jbReturn.put("code", -2);
//            jbReturn.put("messages", "反馈已受理，无法删除！");
//        } else {
//            jbReturn.put("code", 0);
//            jbReturn.put("messages", "操作失败！");
//        }
//        response.getWriter().write(jbReturn.toString());
//    }
//
//    public void updateFeedback(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        response.setContentType("application/json;charset=UTF-8");
//        String data = request.getParameter("data");
//        JSONObject jb = JSONObject.parseObject(data);
//        JSONObject jbReturn = new JSONObject();
//
//        HttpSession session = request.getSession();
//        short character = (short) session.getAttribute("loginType");
//
//        Feedback feedback = JSONObject.toJavaObject(jb.getJSONObject("update"),Feedback.class);
//        if(character == 1){
//            Feedback feedback1 = new Feedback();
//            feedback1.setFbStatus(feedback.getFbStatus());
//            feedback1.setFbId(feedback.getFbId());
//        }else if(character == 0){
//            feedback.setFbStatus(null);
//        }
//        int i = feedbackService.updateFeedback(feedback);
//        if(i > 0){
//            jbReturn.put("code", 1);
//            jbReturn.put("messages", "删除反馈成功");
//        } else if (i < 0) {
//            jbReturn.put("code", -1);
//            jbReturn.put("messages", "关联数据无法修改！");
//        } else {
//            jbReturn.put("code", 0);
//            jbReturn.put("messages", "操作失败！");
//        }
//        response.getWriter().write(jbReturn.toString());
//    }
}
